package com.loongtech.bi.manager.realTime;

import com.loongtech.bi.action.PageHelper;
import com.loongtech.bi.entity.realTime.EntityUserSecondLeftByDay;
import com.loongtech.core.jpa.manager.ManagerQueryCacheBase;
import com.loongtech.core.util.Time;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 实时数据-次留manager
 * 
 * @author marin
 * @date 2015-09-15
 */
@Service
public class UserSecondLeftByDayManager extends ManagerQueryCacheBase<EntityUserSecondLeftByDay> {
	private static final long serialVersionUID = 1L;
	
	// 得到符合平台、渠道、区服条件的查看日次留总和
	public String getAllUserSecondLeftByConditions(Date lookDate, String queryPlatform, String queryChannel, String queryServer) {
		List<Object[]> oo = findByQuery("select day,sum(usercreatenum),sum(loginusernum) from EntityUserSecondLeftByDay where day>=:dayStart and day<:dayEnd and platformid=:platformid and channelid=:channelid and serverid=:serverid ", "dayStart", new Date(Time.getBeginningOfDay(lookDate.getTime() - Time.DAY)), "dayEnd", new Date(Time.getBeginningOfDay(lookDate.getTime())), "platformid", queryPlatform, "channelid", queryChannel, "serverid", queryServer);
		return getResult(oo);
	}
    // 得到符合国家、区服条件的查看日次留总和
    public String getAllUserSecondLeftByConditionsByTag(Date lookDate, String queryPlatform, String queryChannel, String queryServer, String queryCountry) {
        List<Object[]> oo = findByQuery("select day,sum(usercreatenum),sum(loginusernum) from EntityUserSecondLeftByDayByTag where day>=? and day<? and platformid=? and channelid=? and serverid=? and tagvalue = ? ", new Date(Time.getBeginningOfDay(lookDate.getTime() - Time.DAY)), new Date(Time.getBeginningOfDay(lookDate.getTime())), queryPlatform, queryChannel, queryServer, queryCountry);
        return getResult(oo);
    }


	// 得到符合平台、渠道、区服条件的查看日次留总和
	public String getAllMacSecondLeftByConditions(Date lookDate, String queryPlatform, String queryChannel, String queryServer) {
		List<Object[]> oo = findByQuery("select day,sum(maccreatenum),sum(loginmacnum) from EntityMacSecondLeftByDay where day>=:dayStart and day<:dayEnd and platformid=:platformid and channelid=:channelid and serverid=:serverid ", "dayStart", new Date(Time.getBeginningOfDay(lookDate.getTime() - Time.DAY)), "dayEnd", new Date(Time.getBeginningOfDay(lookDate.getTime())), "platformid", queryPlatform, "channelid", queryChannel, "serverid", queryServer);
		return getResult(oo);
	}
	
	// 根据查询结果，拼装最终结果
	private String getResult(List<Object[]> oo) {
		String result = null;
		
		if (oo != null && oo.size() > 0 && oo.get(0)[0] != null) {
			int usercreatenum = 0;
			int loginusernum = 0;
			for(Object[] o : oo) {
				usercreatenum += Integer.parseInt(o[1].toString());
				loginusernum += Integer.parseInt(o[2].toString());
			}
			
			result = PageHelper.double2Percent(loginusernum * 1.0 / (usercreatenum == 0 ? 1 : usercreatenum));
		}
		
		return result;
	}
	
	public List<Object[]> getPaltform2LeftRealTimeDatas(Date lookDate) {
		return findByQuery("select platformid,user_second_left from EntityUserSecondLeftByDay where day=:dayStart and channelid = :channelid and serverid = :serverid", "dayStart", new Date(Time.getBeginningOfDay(lookDate.getTime())), "channelid", "0", "serverid", "0");
	}
	
	public List<Object[]> getPaltform2LeftRealTimeMacDatas(Date lookDate) {
		return findByQuery("select platformid,mac_second_left from EntityMacSecondLeftByDay where day=:dayStart and channelid = :channelid and serverid = :serverid", "dayStart", new Date(Time.getBeginningOfDay(lookDate.getTime())), "channelid", "0", "serverid", "0");
	}
	
	public List<Object[]> getChannel2LeftRealTimeDatas(Date lookDate) {
		return findByQuery("select channelid,user_second_left from EntityUserSecondLeftByDay where day=:dayStart and platformid=:platformid and channelid != :channelid and serverid = :serverid", "dayStart", new Date(Time.getBeginningOfDay(lookDate.getTime())), "platformid", "0", "channelid", "0", "serverid", "0");
	}
	
	public List<Object[]> getChannel2LeftRealTimeMacDatas(Date lookDate) {
		return findByQuery("select channelid,mac_second_left from EntityMacSecondLeftByDay where day=:dayStart and platformid=:platformid and channelid != :channelid and serverid = :serverid", "dayStart", new Date(Time.getBeginningOfDay(lookDate.getTime())), "platformid", "0", "channelid", "0", "serverid", "0");
	}

    public List<Object[]> getServer2LeftRealTimeDatas(Date lookDate) {
        return findByQuery("select serverid,user_second_left from EntityUserSecondLeftByDay where day=:dayStart and platformid=:platformid and channelid = :channelid and serverid != :serverid", "dayStart", new Date(Time.getBeginningOfDay(lookDate.getTime())), "platformid", "0", "channelid", "0", "serverid", "0");
    }

    public List<Object[]> getServer2LeftRealTimeMacDatas(Date lookDate) {
        return findByQuery("select serverid,mac_second_left from EntityMacSecondLeftByDay where day=:dayStart and platformid=:platformid and channelid = :channelid and serverid != :serverid", "dayStart", new Date(Time.getBeginningOfDay(lookDate.getTime())), "platformid", "0", "channelid", "0", "serverid", "0");
    }
}
